package io.renren.modules.styj1.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.Date;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.SysDept1Entity;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysDept1Service;
import io.renren.modules.sys.service.SysUserRoleService;
import io.renren.modules.sys.service.SysUserService;
import io.renren.modules.sys.service.impl.SysUserRoleServiceImpl;
import io.renren.modules.sys.shiro.ShiroUtils;
import io.renren.modules.styj1.entity.B11GreenConstructionRateEntity;
import io.renren.modules.styj1.service.B11GreenConstructionRateService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author Liu Yunchang
 * @email yunchang75@aliyun.com
 * @date 2020-08-23 14:34:52
 */
@RestController
@RequestMapping("styj1/b11greenconstructionrate")
public class B11GreenConstructionRateController {
    @Autowired
    private B11GreenConstructionRateService b11GreenConstructionRateService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysDept1Service sysDept1Service;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("styj1:b11greenconstructionrate:list")
    public R list(@RequestParam Map<String, Object> params){
        String createby=ShiroUtils.getUserId().toString();
        params.put("createby",createby);
        PageUtils page = b11GreenConstructionRateService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
        * 列表
        */
    @RequestMapping("/listdistrict")
    @RequiresPermissions("styj1:b11greenconstructionrate:listdistrict")
    public R listdistrict(@RequestParam Map<String, Object> params){

        long id=ShiroUtils.getUserId();
        SysUserEntity user=sysUserService.getById(id);
        Long deptid=user.getDeptId();
        SysDept1Entity sysDept1Entity=sysDept1Service.getById(deptid);
        String typecode=sysDept1Entity.getDeptcode();
        System.out.println("typecode is "+typecode+"**********");
        params.put("typecode",typecode);
        PageUtils page = b11GreenConstructionRateService.queryPageforDistrict(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{greenId}")
    @RequiresPermissions("styj1:b11greenconstructionrate:info")
    public R info(@PathVariable("greenId") Integer greenId){
        B11GreenConstructionRateEntity b11GreenConstructionRate = b11GreenConstructionRateService.getById(greenId);

        return R.ok().put("b11GreenConstructionRate", b11GreenConstructionRate);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("styj1:b11greenconstructionrate:save")
    public R save(@RequestBody B11GreenConstructionRateEntity b11GreenConstructionRate){

        long id=ShiroUtils.getUserId();

        SysUserEntity user=sysUserService.getById(id);
        String createby=user.getUserId().toString();

        b11GreenConstructionRate.setCreatby("" + createby);
        Date createat=new Date(System.currentTimeMillis());
        b11GreenConstructionRate.setCreatat(createat);

        Long deptid=user.getDeptId();
        SysDept1Entity sysDept1Entity=sysDept1Service.getById(deptid);
        String parentcode=sysDept1Entity.getParentId().toString();
        b11GreenConstructionRate.setParentCode(parentcode);

        String typecode=sysDept1Entity.getDeptcode();
        b11GreenConstructionRate.setTypeCode(typecode);
        b11GreenConstructionRateService.save(b11GreenConstructionRate);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("styj1:b11greenconstructionrate:update")
    public R update(@RequestBody B11GreenConstructionRateEntity b11GreenConstructionRate){
        ValidatorUtils.validateEntity(b11GreenConstructionRate);
        b11GreenConstructionRateService.updateById(b11GreenConstructionRate);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("styj1:b11greenconstructionrate:delete")
    public R delete(@RequestBody Integer[] greenIds){
        b11GreenConstructionRateService.removeByIds(Arrays.asList(greenIds));

        return R.ok();
    }

}
